package com.skins{
	import com._public._images.ScaleBitmap;
	import com._public._net.LoadData;
	import com.skins.data.CssVo;
	import com.skins.events.SkinLoadEvent;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.events.EventDispatcher;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	/**
	 * 样式图片加载组件
	 * @author WZH(shch8.com)
	 */
	public class ImgSkinLoader extends ScaleBitmap {
		private var classAry:Array
		private var _className:String;
		private var bitmapClip:Bitmap
		private var dataPool:BitmapDataPool = BitmapDataPool.getInstance();
		public var imgName:String;
		public var eventPatch:EventDispatcher = new EventDispatcher();

		public function ImgSkinLoader(className:String=null, defaultPath:String=null){
			var rect:Rectangle = getCssAry(className.split(CssVo.DIS_ITEM)[1]);
			if (rect == null)
				rect = new Rectangle(0, 0, 100, 50);
			super(new BitmapData(rect.width, rect.height, false));
			_className = className;
			if (_className.indexOf(CssVo.IMG) == -1){
				trace("未找到图片标识");
				return;
			}
			classAry = _className.split(CssVo.DIS_ITEM);
			imgName = classAry[0];
			var bd:BitmapData = dataPool.getBitmapData(imgName);
			if (bd != null){
				addBitmap(bd);
				//setSize(this.width, this.height);
				return
			} else if (dataPool.isLoading(imgName)){ //正在加载图片时，收到获取图片的命令，转做事件监听
				dataPool.addEventListener(SkinLoadEvent.LOAD_COMPLETE, poolLoadComplete)
				return
			}
			if (imgName.indexOf("http://") == -1){
				defaultPath = defaultPath + imgName
			} else {
				defaultPath = imgName;
			}
			dataPool.push(imgName, null);
			//trace("===",defaultPath)
			LoadData.getInstance().loadFile(defaultPath, loadComplete, loadError)
		}

		/**
		 * 位图数据加载完成
		 */
		private function loadComplete(loader:Loader):void {
			var bitmap:Bitmap = loader.content as Bitmap;
			dataPool.push(imgName, bitmap.bitmapData.clone());
			addBitmap(bitmap.bitmapData);
			dataPool.dispatchEvent(new SkinLoadEvent(SkinLoadEvent.LOAD_COMPLETE, imgName));
			//setSize(this.width, this.height)
			eventPatch.dispatchEvent(new SkinLoadEvent(SkinLoadEvent.LOAD_COMPLETE,imgName))
		}

		/**
		 * 位图池相同数据加载完成
		 */
		private function poolLoadComplete(e:SkinLoadEvent):void {
			if (e.id == imgName){
				var bd:BitmapData = dataPool.getBitmapData(imgName);
				if (bd != null){
					addBitmap(bd);
					//setSize(this.width,this.height)
					dataPool.removeEventListener(SkinLoadEvent.LOAD_COMPLETE, poolLoadComplete)
				} else {
					loadError();
				}
			}
		}

		private function addBitmap(bd:BitmapData):void {
			var rect:Rectangle = getCssAry(classAry[1], false)
			if (rect == null){ //无位移图片分切
				bitmapData = bd.clone();
			} else {
				//trace("复制皮肤")
				var newBd:BitmapData = new BitmapData(rect.width, rect.height);
				newBd.copyPixels(bd, rect, new Point());
				bitmapData = newBd;
			}
			if (_className.indexOf(CssVo.DIS_ITEM + CssVo.NINE) == -1)
				return; //无9切片模式
			rect = getCssAry(classAry[2]);
			if (rect == null)
				rect = new Rectangle(bitmapData.width * 0.25, bitmapData.height * 0.25, bitmapData.width * 0.5, bitmapData.height * 0.5);
			scale9Grid = rect;
			rect = getCssAry(classAry[1]); //2012/11/23 14:09
			if (cssWid)
				width = rect.width;
			if (cssHei)
				height = rect.height; //外部没设置时，使用CSS中的尺寸
		}

		
		private function loadError():void {
			trace("加载失败:", imgName)
			bitmapData = new BitmapData(100, 100, false, 0x999999)
		}

		/**
		 * 获取样式中图片切割位置转化
		 * @param	idStr		格式:img(0,0,58,60)
		 * @param	getAll		是否获取全路径，两位时，不做切割处理,如：img( 58,60)
		 * @return
		 */
		private function getCssAry(idStr:String, getAll:Boolean = true):Rectangle {
			var ary:Array = idStr.slice(idStr.indexOf("(") + 1, idStr.lastIndexOf(")")).split(",");
			if (ary.length == 2 && getAll)
				return new Rectangle(0, 0, ary[0], ary[1]);
			if (ary.length < 4)
				return null
			return new Rectangle(ary[0], ary[1], ary[2], ary[3]);
		}
	}
}